package day02;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import utils.JDBCUtils;

import java.sql.Connection;
import java.sql.SQLException;

public class ComboPoolDemo {
    //xml配置文件创建连接池
    public static void main(String[] args) {
        //空参构造方法连接的是默认配置项
        //ComboPooledDataSource cpds = new ComboPooledDataSource();
        //有参可以连接指定配置项
        ComboPooledDataSource cpds = new ComboPooledDataSource("dalwin_c3p0");
        Connection con;
        try {
            con = cpds.getConnection();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        System.out.println("连接池连接对象："+con);
        try {
            con.close();
            cpds.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        //正常连接对象与连接池连接对象的区别
        Connection con2 = JDBCUtils.getConnection();
        System.out.println("正常连接对象："+con2);
        try {
            con2.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        System.out.println("----------------------------");
        System.out.println("关闭后连接池连接对象："+con);
        System.out.println("关闭连接池后连接池对象："+cpds);
        System.out.println("关闭后正常连接对象："+con2);
    }
}
